package com.gaogzhen.service.impl;

import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.domain.User;
import com.gaogzhen.dto.UserBankDto;
import com.gaogzhen.mappers.UserBankDtoMapper;
import com.gaogzhen.service.UserService;
import com.mysql.cj.jdbc.exceptions.SQLError;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaogzhen.domain.UserBank;
import com.gaogzhen.mapper.UserBankMapper;
import com.gaogzhen.service.UserBankService;
/**
* @description ${description}
* @author Administrator
* @date 2022-06-07 11:46
* @version 1.0
*/
@Service
public class UserBankServiceImpl extends ServiceImpl<UserBankMapper, UserBank> implements UserBankService{

    @Autowired
    private UserService userService;

    @Override
    public Page<UserBank> findByPage(Page<UserBank> page, Long userId) {
        page.addOrder(OrderItem.desc("last_update_time"));
        return page(page, Wrappers.lambdaQuery(UserBank.class)
                            .eq(UserBank::getUserId, userId)
        );
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        boolean update = update(Wrappers.lambdaUpdate(UserBank.class)
                .eq(UserBank::getId, id)
                .set(UserBank::getStatus, status)
        );
        if (!update) {
            throw new RuntimeException("更新状态失败！");
        }
    }

    @Override
    public void updateUserBank(UserBank userBank) {
        boolean update = updateById(userBank);
        if (!update) {
            throw new RuntimeException("更新银行卡失败！");
        }
    }

    @Override
    public UserBank getCurrentUserBank(Long currentUserId) {
         return getOne(new LambdaQueryWrapper<UserBank>()
                .eq(UserBank::getUserId, currentUserId)
                .eq(UserBank::getStatus, 1));
    }

    @Override
    public void bindBank(Long userId, UserBank userBank) {
        // 交易支付密码
        User user = userService.getById(userId);
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        if (!passwordEncoder.matches(userBank.getPayPassword(), user.getPaypassword())) {
            throw new IllegalArgumentException("用户支付密码错误");
        }
        userBank.setUserId(userId);
        userBank.setStatus((byte) 0);
        boolean update = saveOrUpdate(userBank);
        if (!update) {
            throw new RuntimeException("更新银行卡错误");
        }
    }

    @Override
    public UserBankDto getBriefUserBank(Long userId) {
        UserBank userBank = getOne(new LambdaQueryWrapper<UserBank>()
                .eq(UserBank::getUserId, userId)
                .eq(UserBank::getStatus, 1)
        );
        if (userBank == null) {
            return null;
        }
        return UserBankDtoMapper.INSTANCE.convert2Dto(userBank);
    }
}
